import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/login/index.vue'
import layout from '../views/layout/index.vue'
import Article from '../views/article/index.vue'
import page404 from '../views/page404/index.vue'
import EditArticle from '../views/edit/index.vue'
import sucai from '../views/image/index.vue'
import addAr from '../views/addArticle/index.vue'
import comment from '../views/comment/index.vue'
import fans from '../views/fans/index.vue'
Vue.use(VueRouter)

const routes = [ 
  {
    path: '/login',
    component: Login
  },
  {
    path: '/',
    component: layout,
    children:[
      {
        path: '/articles',
        component:Article
      },
      {
        path:'/editArticle/:id',// 动态路由
        name:'editArticle',
        component:EditArticle
      },
      {
        path:'/image',
        name:'sucai',
        component:sucai
      },
      {
        path:'/addArticle',
        name:'addAr',
        component:addAr
      },
      {
        path:'/comment',
        name:'comment',
        component:comment
      },
      {
        path:'/fans',
        name:'fans',
        component:fans
      }
    ]
  },
 
  {
    path:"*",
    component:page404
  }
]

// Add a request interceptor
// axios.interceptors.request.use(function (config) {
//   if(tokenInfo && tokenInfo.token){
//     config.headers.Authorization = `Bearer ${tokenInfo.token}`
//   }
  
//   return config;
// }, function (error) {
//   // Do something with request error
//   return Promise.reject(error);
// });

const router = new VueRouter({
  routes
})
const tokenInfo = localStorage.getItem('tokenInfo')
router.beforeEach((to,from,next)=>{
console.log(to,from);
  if(to.path==='/'){
    if(tokenInfo){
      next()
    }else{
      next('/login')
    }
  }else{
    next()
  }
})
export default router
